package mesweb.mesweb.controller.procedure;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.dto.Procedure.ProductProduceProcedureDto;
import mesweb.mesweb.entity.Procedure.MesProcedure;
import mesweb.mesweb.entity.Procedure.MesProductProcedureBranchProcedure;
import mesweb.mesweb.service.procedure.IMesProductProcedureBranchProcedureService;
import mesweb.mesweb.vo.procecdure.MesProductProcedureBranchProcedureVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

/**
* @Description: 生产支线工序关系
* @Author: jeecg-boot
* @Date:   2021-06-26
* @Version: V1.0
*/

@Api(tags="生产支线工序关系")
@RestController
@RequestMapping("/mes/mesProductProcedureBranchProcedure")
public class MesProductProcedureBranchProcedureController {
   @Autowired
   private IMesProductProcedureBranchProcedureService mesProductProcedureBranchProcedureService;


   @GetMapping("/GetAll")
   @ApiOperation("获取全部数据列表")
   public CommonResult<?> GetAll(@ApiParam("名称") @RequestParam( value = "name",required = false) String name) {
       List<MesProductProcedureBranchProcedureVo> data;
       QueryWrapper<MesProductProcedureBranchProcedureVo> queryWrapper = new QueryWrapper<>();
       if (StringUtils.isBlank(name) || name.equals("全部")) {

       } else {
           }
       data = mesProductProcedureBranchProcedureService.getProductProcedureBranchProcedure(queryWrapper);
       return CommonResult.success(data);
   }
   /**
    * 分页列表查询
    *
    * @param mesProductProcedureBranchProcedure
    * @param page
    * @param page
    * @param req
    * @return
    */
   @ApiOperation(value="生产支线工序关系-分页列表查询", notes="生产支线工序关系-分页列表查询")
   @GetMapping(value = "/list")
   public CommonResult<?> queryPageList(MesProductProcedureBranchProcedure mesProductProcedureBranchProcedure,
                                  @RequestParam(name="page", defaultValue="1") Integer page,
                                  @RequestParam(name="size", defaultValue="10") Integer size,
                                  HttpServletRequest req) {
       QueryWrapper<MesProductProcedureBranchProcedureVo> queryWrapper = new QueryWrapper<>();
       Page<MesProductProcedureBranchProcedureVo> pageMesProductProcedureBranchProcedure = new Page<>(page, size);
       IPage<MesProductProcedureBranchProcedureVo> pageList = mesProductProcedureBranchProcedureService.getProductProcedureBranchProcedure(pageMesProductProcedureBranchProcedure, queryWrapper);
       return CommonResult.success(pageList);
   }
   /**
    * 编辑
    *
    * @param mesProductProcedureBranchProcedure
    * @return
    */
   @ApiOperation(value="生产支线工序关系-编辑", notes="生产支线工序关系-编辑")
   @PostMapping(value = "/save")
   public CommonResult<?> edit(@RequestBody MesProductProcedureBranchProcedure mesProductProcedureBranchProcedure) {
      boolean result = false;
       if(mesProductProcedureBranchProcedure.getId() == 0L) {
            result = mesProductProcedureBranchProcedureService.save(mesProductProcedureBranchProcedure);
       }
       else
       {
           result = mesProductProcedureBranchProcedureService.updateById(mesProductProcedureBranchProcedure);
       }
       return CommonResult.success(mesProductProcedureBranchProcedure);
   }
    /**
     * 编辑
     *
     * @param mesProductProcedureBranchProcedures
     * @return
     */
    @ApiOperation(value="生产工序支线和工序关系-批量增加", notes="生产工序支线和工序关系-批量增加")
    @PostMapping(value = "/saveBatch")
    public CommonResult<?> edit(@RequestBody Collection<MesProductProcedureBranchProcedure> mesProductProcedureBranchProcedures) {
        boolean result = false;
        try {
            if (mesProductProcedureBranchProcedures.size() > 0L) {
                for (MesProductProcedureBranchProcedure mesProductProcedureBranchProcedure :
                        mesProductProcedureBranchProcedures) {
                    result = mesProductProcedureBranchProcedureService.save(mesProductProcedureBranchProcedure);
                }
            }
            return CommonResult.success(mesProductProcedureBranchProcedures);
        }catch (Exception e)
        {
            return  CommonResult.failed(e.getMessage());
        }
    }

   /**
    * 通过id删除
    *
    * @param id
    * @return
    */
   @ApiOperation(value="生产支线工序关系-通过id删除", notes="生产支线工序关系-通过id删除")
   @DeleteMapping(value = "/delete")
   public CommonResult<?> delete(@RequestParam(name="id",required=true) String id) {
       mesProductProcedureBranchProcedureService.removeById(id);
       return CommonResult.success("删除成功!");
   }

   /**
    * 批量删除
    *
    * @param ids
    * @return
    */
   @ApiOperation(value="生产支线工序关系-批量删除", notes="生产支线工序关系-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public CommonResult<?> deleteBatch(@ApiParam("MesProductProcedureBranchProcedureIDs") @RequestBody List<Long> ids) {
       this.mesProductProcedureBranchProcedureService.removeByIds(ids);
       return CommonResult.success("批量删除成功！");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @ApiOperation(value="生产支线工序关系-通过id查询", notes="生产支线工序关系-通过id查询")
   @GetMapping(value = "/queryById")
   public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
       MesProductProcedureBranchProcedure mesProductProcedureBranchProcedure = mesProductProcedureBranchProcedureService.getById(id);
       return CommonResult.success(mesProductProcedureBranchProcedure);
   }

    @GetMapping("/getProductProcedureProcedures")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> getProductProcedureProcedures(@ApiParam("产品工序线Id") @RequestParam( value = "productProcedureId",required = false,defaultValue = "0") Long productProcedureId) {
        List<MesProcedure> data;
        ProductProduceProcedureDto productProduceProcedureDto = new ProductProduceProcedureDto();
        productProduceProcedureDto.setProductProduceProcedureId(productProcedureId);
        productProduceProcedureDto.setSourceType(1);
        data = mesProductProcedureBranchProcedureService.getProductProcedureProcedures(productProduceProcedureDto);
        return CommonResult.success(data);
    }
}
